package com.ruoyi.business.controller;

import com.ruoyi.business.dto.AddApplicationDTO;
import com.ruoyi.business.dto.AuditDTO;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.Application;
import com.ruoyi.business.service.IApplicationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 申请Controller
 * 
 * @author lcz
 * @date 2025-01-19
 */
@Api(tags = "申请相关接口")
@RestController
@RequestMapping("/business/application")
public class ApplicationController extends BaseController
{
    @Autowired
    private IApplicationService applicationService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询申请列表
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:application:list')")
    @GetMapping("/list")
    public TableDataInfo list(Application application)
    {
        startPage();
        List<Application> list = applicationService.selectApplicationList(application);
        return getDataTable(list);
    }

    /**
     * 导出申请列表
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:application:export')")
    @Log(title = "申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Application application)
    {
        List<Application> list = applicationService.selectApplicationList(application);
        ExcelUtil<Application> util = new ExcelUtil<Application>(Application.class);
        util.exportExcel(response, list, "申请数据");
    }

    /**
     * 获取申请详细信息
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:application:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(applicationService.selectApplicationById(id));
    }

    /**
     * 新增申请
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:application:add')")
    @Log(title = "申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Application application)
    {
        return toAjax(applicationService.insertApplication(application));
    }

    /**
     * 修改申请
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:application:edit')")
    @Log(title = "申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Application application)
    {
        return toAjax(applicationService.updateApplication(application));
    }

    /**
     * 删除申请
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:application:remove')")
    @Log(title = "申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(applicationService.deleteApplicationByIds(ids));
    }

    /** 前端接口 ***************************************************************************************************/
    /** 前端接口 ***************************************************************************************************/
    /** 前端接口 ***************************************************************************************************/

    /**
     * 查询申请列表
     * 接口路径：/application/front/list
     */
    @GetMapping("/front/list")
    @ApiOperation("查询申请单列表")
    public TableDataInfo<Application> frontList(Application application)
    {
        Long enterpriseId = tokenService.getUserId();
        application.setEnterpriseId(enterpriseId);
        startPage();
        List<Application> list = applicationService.selectApplicationList(application);
        return getDataTable(list);
    }

    /**
     * 获取申请详细信息
     * 接口路径：/application/front/info
     */
    @ApiOperation("获取申请单详细信息")
    @GetMapping(value = "/front/info")
    public R<Application> frontInfo(@RequestParam("id") Long id)
    {
        return R.ok(applicationService.selectApplicationById(id));
    }

    /**
     * 创建抵消申请单接口
     * 接口路径：/application/front/add
     */
    @ApiOperation("创建抵消申请单接口")
    @PostMapping("/front/add")
    public AjaxResult addApplication(@RequestBody AddApplicationDTO dto)
    {
        return applicationService.addApplication(dto);
    }

//    /**
//     * 修改申请
//     * 接口路径：/application/front/edit
//     */
//    @ApiOperation("前台修改申请")
//    @PostMapping("/front/edit")
//    public AjaxResult frontEdit(@RequestBody Application application)
//    {
//        return toAjax(applicationService.updateApplication(application));
//    }
//
//    /**
//     * 删除申请
//     * 接口路径：/application/front/delete
//     */
//    @ApiOperation("前台删除申请")
//    @GetMapping("/front/delete")
//    public AjaxResult frontRemove(@RequestParam Long[] ids)
//    {
//        return toAjax(applicationService.deleteApplicationByIds(ids));
//    }
}
